package hars

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-form/catch"
	"order-form/models"
	"order-form/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	name := c.PostForm("name")
	password := c.PostForm("password")
	if name == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名密码不正确",
			"data": nil,
		})
		return
	}
	redis_login := "redis_login" + name
	catchcount, _ := catch.Rdb.Get(catch.Ctx, redis_login).Int()
	if catchcount > 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
			"data": nil,
		})
		return
	}
	var user models.User
	err := models.DB.Where("name = ?", name).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名不正确",
			"data": nil,
		})
		return
	}
	if user.Password != pkg.MD5(password) {
		catch.Rdb.Incr(catch.Ctx, redis_login)
		if catchcount == 2 {
			catch.Rdb.Set(catch.Ctx, redis_login, 5, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次禁止登录",
				"data": nil,
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	nid, _ := strconv.Atoi(c.Query("nid"))
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return

}
func UserRole(c *gin.Context) {
	uid := c.Query("uid")
	/*SELECT
	*
		FROM
	roles
	LEFT JOIN user_roles ON user_roles.rid = roles.id
	WHERE
	user_roles.uid = 1;

	*/

	var roles models.Role
	models.DB.Table("roles").Where("LEFT JOIN user_roles ON user_roles.rid = roles.id").Joins("user_roles.uid = ?", uid).Debug().
		Find(&roles)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": roles,
	})
	return
}
func NodeRole(c *gin.Context) {
	rid := c.Query("rid")
	/*	SELECT
		*
			FROM
		nodes
		LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		WHERE
		role_nodes.rid = 1;

	*/
	var nodes models.Node
	models.DB.Table("nodes").Select("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid = ?", rid).Debug().Find(&nodes)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": nodes,
	})
	return

}
func AddOrder(c *gin.Context) {
	orderno := c.PostForm("orderno")
	ordername := c.PostForm("ordername")
	orderprice, _ := strconv.Atoi(c.PostForm("orderprice"))
	order := models.Order{
		OrderNo:    orderno,
		OrderName:  ordername,
		OrderPrice: orderprice,
	}
	models.DB.Create(&order)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	return

}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.PostForm("page"))
	size, _ := strconv.Atoi(c.PostForm("size"))
	offser := (page - 1) * size
	var order []models.Order
	redis_list := "redis_list" + strconv.Itoa(page)
	catchcount, _ := catch.Rdb.Get(catch.Ctx, redis_list).Result()
	if catchcount == "" {
		models.DB.Offset(offser).Limit(size).Find(&order)
		jsondata, _ := json.Marshal(&order)
		catch.Rdb.Set(catch.Ctx, redis_list, jsondata, time.Hour*2)

	} else {
		json.Unmarshal([]byte(catchcount), &order)
	}

	var toal int64
	models.DB.Model(models.Order{}).Count(&toal)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	return

}
func RepoLimit(c *gin.Context) {
	id := c.Query("id")
	var repo models.Repo
	redis_limmit := "redis_limmit" + id
	catchcount, _ := catch.Rdb.Get(catch.Ctx, redis_limmit).Result()
	if catchcount == "" {
		models.DB.Where("id =?", id).First(&repo)
		jsondata, _ := json.Marshal(&repo)
		catch.Rdb.Set(catch.Ctx, redis_limmit, jsondata, time.Hour*2)
	} else {
		json.Unmarshal([]byte(catchcount), &repo)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": repo,
	})
	return
}
func List(c *gin.Context) {
	var order []models.Order
	models.DB.Find(&order)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": order,
	})
	return
}
